package com.yang.inventorymanagement.controllerview.products;

import com.yang.inventorymanagement.pojo.Inventory;
import com.yang.inventorymanagement.pojo.Products;
import com.yang.inventorymanagement.pojo.Suppliers;
import com.yang.inventorymanagement.service.InventoryService;
import com.yang.inventorymanagement.service.ProductsService;
import com.yang.inventorymanagement.service.SuppliersService;
import com.yang.inventorymanagement.utils.SpringContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.swing.*;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

@Lazy
@Component("ProductsAddSwing")
@Scope("prototype")
/**
 * 商品新增界面
 */
public class ProductsAddSwing extends JDialog {


    JPanel panel = new JPanel();

    private JPanel contentPane;
    private JTextField contactEmailField;


    private JTextField suppliersNameField;
    //库存service
    private SuppliersService suppliersService = SpringContextUtils.getBean(SuppliersService.class);
    //库存中间表
    private InventoryService inventoryService = SpringContextUtils.getBean(InventoryService.class);
    //商品表
    private ProductsService productsService = SpringContextUtils.getBean(ProductsService.class);



    public ProductsAddSwing() {

        setModal(true);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
        //设置窗口大小
        setBounds(100, 100, 573, 404);
        //设置居中显示
        setLocationRelativeTo(null);
        //设置不可编辑
        setResizable(false);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        Border titleBorder = BorderFactory.createTitledBorder("商品入库（新）");
        contentPane.setBorder(titleBorder);
        setContentPane(contentPane);
        JLabel jLabel = new JLabel("添加商品");
        jLabel.setBounds(210, 10, 142, 32);
        jLabel.setFont(new Font("宋体", Font.PLAIN, 24));


        JLabel suppliersNameLable = new JLabel("商品");
        suppliersNameLable.setBounds(136, 68, 80, 15);
        //用户输入框
        suppliersNameField = new JTextField();
        //userNameField.setColumns(10);
        suppliersNameField.setBounds(197, 65, 150, 21);
        contentPane.add(suppliersNameField);

        JLabel contactName = new JLabel("供应商");
        contactName.setBounds(136, 98, 80, 15);
        contentPane.add(contactName);
        // 初始化 JComboBox
        JComboBox<String> comboBox = new JComboBox<>();
        List<Suppliers> suppliers = suppliersService.suppliersGetallList();
        if (suppliers.size() > 0) {
            for (Suppliers item : suppliers) {
                comboBox.addItem(item.getSuppliername());
            }
        }
        comboBox.setBounds(197, 95, 150, 21);
        contentPane.add(comboBox);


        //用户输入框
       /* contactNameField = new JTextField();
        //userNameField.setColumns(10);
        contactNameField.setBounds(197, 95, 150, 21);
        contentPane.add(contactNameField);*/

        JLabel emailLable = new JLabel("库存数量");
        emailLable.setBounds(136, 128, 80, 15);
        contentPane.add(emailLable);

        contactEmailField = new JTextField();
        contactEmailField.setColumns(10);
        contactEmailField.setBounds(197, 125, 150, 21);
        contentPane.add(contactEmailField);

        contentPane.setLayout(null);
        contentPane.add(jLabel);
        contentPane.add(suppliersNameLable);


        JButton btnSubmit = new JButton("新增");
        //新增用户信息
        btnSubmit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (JOptionPane.showConfirmDialog(null, "确定新增？") == 0) {
                    if (!contactEmailField.getText().matches("\\d+")){
                        JOptionPane.showMessageDialog(null, "填写错误，请重新填写", "提示", JOptionPane.ERROR_MESSAGE);
                        return;
                    }


                    //商品表
                    Products products = new Products();
                    //商品名称
                    products.setProductname(suppliersNameField.getText());
                    //库存数量
                    products.setQuantity(Integer.valueOf(contactEmailField.getText()));
                    int i = productsService.insertProducts(products);
                    //库存表
                    Inventory inventory = new Inventory();
                    //库存数量
                    inventory.setQuantity(Integer.valueOf(contactEmailField.getText()));
                    //供应商id
                    String selectedItem = (String) comboBox.getSelectedItem();//供应商名称
                    Suppliers suppliers1 = suppliersService.selectSuppliersBySupplierName(selectedItem);
                    inventory.setSupplierid(suppliers1.getSupplierid());
                    //商品id
                    Integer productid = products.getProductid();//商品自增id
                    inventory.setProductid(productid);
                    int i1 = inventoryService.insertInventory(inventory);

                    //新增
                    // int result = suppliersService.insertSuppliers(suppliers);
                    if (i1 > 0) {
                        JOptionPane.showMessageDialog(null, "新增成功", "提示", JOptionPane.INFORMATION_MESSAGE);
                        // SessionUtils.getSession().put("user", userService.getById(userId));
                        setVisible(true);
                        dispose();
                    } else {
                        JOptionPane.showMessageDialog(null, "新增失败", "提示", JOptionPane.ERROR_MESSAGE);
                    }
                }
            }
        });
        btnSubmit.setBounds(71, 322, 93, 23);
        contentPane.add(btnSubmit);


    }
}
